﻿#include "Card.h"
#include "Hand.h"
#include "Deck.h"
#include <string>

#define ╔ <<(char)201
#define ═ <<(char)205
#define ╗ <<(char)187
#define ║ <<(char)186
#define ╚ <<(char)200
#define ╝ <<(char)188
#define ░ <<(char)176

#define s <<" "
#define _n <<std::endl
#define $DRAW std::cout
#define $a <<(char)(h1.HandCards[0].Symbol + 3)
#define $b <<(char)(h1.HandCards[1].Symbol + 3)
#define $c <<(char)(h1.HandCards[2].Symbol + 3)
#define $d <<(char)(h1.HandCards[3].Symbol + 3)
#define $e <<(char)(h1.HandCards[4].Symbol + 3)
#define $1 <<(char)(h1.HandCards[0].Number)
#define $2 <<(char)(h1.HandCards[1].Number)
#define $3 <<(char)(h1.HandCards[2].Number)
#define $4 <<(char)(h1.HandCards[3].Number)
#define $5 <<(char)(h1.HandCards[4].Number)
#define $f <<(char)(h2.HandCards[0].Symbol + 3)
#define $g <<(char)(h2.HandCards[1].Symbol + 3)
#define $h <<(char)(h2.HandCards[2].Symbol + 3)
#define $i <<(char)(h2.HandCards[3].Symbol + 3)
#define $j <<(char)(h2.HandCards[4].Symbol + 3)
#define $6 <<(char)(h2.HandCards[0].Number)
#define $7 <<(char)(h2.HandCards[1].Number)
#define $8 <<(char)(h2.HandCards[2].Number)
#define $9 <<(char)(h2.HandCards[3].Number)
#define $0 <<(char)(h2.HandCards[4].Number)

#define SPACEBETWENHANDS 33
#define HANDTOCONSOLE 23

using namespace std;

void InitHand(hand &h)
{
	h.nbCards = 0;
}

void GiveHand(hand &h, deck &d)
{
	if(h.nbCards == 0)
	{
		h.HandCards[h.nbCards] = d.Cards[0];
		RemoveCard(d);
		h.nbCards++;

		h.HandCards[h.nbCards] = d.Cards[0];
		RemoveCard(d);
		h.nbCards++;
	}
	else
	{
		std::cout << "Main deja initialisee" << std::endl;
	}
}

void SplitHand(hand &h, hand &sh)
{
	InitHand(sh);
	sh.HandCards[0] = h.HandCards[1];
	h.nbCards--;
	sh.nbCards++;
}

void ShowHand(hand &h1)
{
	int lineLenght;
	lineLenght = ((h1.nbCards - 1)* 4) + 7;

	for(int v = 0; v < 7; v++)
	{
		for(int z = 0; z < HANDTOCONSOLE; z++)
		{
			std::cout << " ";
		}

		bool lastCard = false;
		int hr = 0;
		int cardIndex = 0;

		for(int h = 0; h < (lineLenght); h++)
		{
			if(!lastCard)
			{
				std::cout << h1.HandCards[cardIndex].D[hr][v];
				hr++;
				if(h >= 3)
				{
					cardIndex = 1;
					hr = h-3;
					if(h1.nbCards > 2)
					{
						if(h >= 7)
						{
							cardIndex = 2;
							hr = h - 7;
							if(h1.nbCards > 3)
							{
								if(h >= 11)
								{
									cardIndex = 3;
									hr = h - 11;
								}
							}
						}
					}
				}
				if(h >= (lineLenght - 8))
				{
					lastCard = true;
					hr = 0;
				}
			}
			else
			{
				//derniere carte player 1
				std::cout << h1.HandCards[h1.nbCards-1].D[hr][v];
				hr++;
			}
		}
			std::cout << std::endl;
	}
}

void ShowHand(hand &h1, hand &h2)
{
	int lineLenght;
	int lineLenght2;
	lineLenght = ((h1.nbCards - 1)* 4) + 7;
	lineLenght2 = ((h2.nbCards - 1)* 4) + 7;

	for(int v = 0; v < 7; v++)
	{
		bool lastCard = false;
		bool lastCard2 = false;
		bool hand2 = false;
		int hr = 0;
		int cardIndex = 0;

		for(int h = 0; h < (lineLenght+SPACEBETWENHANDS+lineLenght2); h++)
		{
			if(!hand2)
			{
				if(!lastCard)
				{
					std::cout << h1.HandCards[cardIndex].D[hr][v];
					hr++;
					if(h >= 3)
					{
						cardIndex = 1;
						hr = h-3;
						if(h1.nbCards > 2)
						{
							if(h >= 7)
							{
								cardIndex = 2;
								hr = h-7;
								if(h1.nbCards > 3)
								{
									if(h >= 11)
									{
										cardIndex = 3;
										hr = h-11;
									}
								}
							}
						}
					}
					if(h >= (lineLenght - 8))
					{
						lastCard = true;
					}
				}
				else
				{
					//derniere carte player 1
					std::cout << h1.HandCards[h1.nbCards - 1].D[h-(lineLenght - 7)][v];

					if(h >= lineLenght - 1)
					{
						for(int z = 0;z < SPACEBETWENHANDS; z++)
						{
							std::cout << " ";
							h++;
						}
						hand2 = true;
						hr = 0;
						cardIndex = 0;
					}
				}
			}
			else
			{
				if(!lastCard2)
				{
					std::cout << h2.HandCards[cardIndex].D[hr][v];
					hr++;
					if(h >= 3 + (lineLenght + SPACEBETWENHANDS))
					{
						cardIndex = 1;
						hr = h - (lineLenght + SPACEBETWENHANDS + 3);
						if(h >= 7 +(lineLenght + SPACEBETWENHANDS))
						{
							cardIndex = 2;
							hr = h - (lineLenght + SPACEBETWENHANDS + 7);
							if(h >= 11 +(lineLenght + SPACEBETWENHANDS))
							{
								cardIndex = 3;
								hr = h - (lineLenght + SPACEBETWENHANDS + 11);
							}
						}
					}
					if(h >= ((lineLenght + SPACEBETWENHANDS) + lineLenght2 - 8))
					{
						lastCard2 = true;
						hr = 0;
					}
				}
				else
				{
					std::cout << h2.HandCards[h2.nbCards - 1].D[h -((lineLenght2 + lineLenght + SPACEBETWENHANDS) - 7)][v];
				}
			}
		}
		std::cout << std::endl;
	}
}

void ShowDealer(hand h1)
{
	char E[7][7] = 
	{
		{ (char)201 , (char)205 , (char)205 , (char)205 , (char)205 , (char)205 , (char)187 },
		{ (char)186 , (char)177 , (char)177 , (char)177 , (char)177 , (char)177 , (char)186 },
		{ (char)186 , (char)177 , (char)177 , (char)177 , (char)177 , (char)177 , (char)186 },
		{ (char)186 , (char)177 , (char)177 , (char)177 , (char)177 , (char)177 , (char)186 },
		{ (char)186 , (char)177 , (char)177 , (char)177 , (char)177 , (char)177 , (char)186 },
		{ (char)186 , (char)177 , (char)177 , (char)177 , (char)177 , (char)177 , (char)186 },
		{ (char)200 , (char)205 , (char)205 , (char)205 , (char)205 , (char)205 , (char)188 }
	};


	for(int v = 0; v < 7; v++)
	{
		for(int z = 0; z < HANDTOCONSOLE; z++)
		{
			std::cout << " ";
		}

		bool lastCard = false;
		int hr = 0;

		for(int h = 0; h < 14; h++)
		{
			if(!lastCard)
			{
				std::cout << E[v][h];
				hr++;

				if(h >= 6)
				{
					lastCard = true;
					hr = 0;
				}
			}
			else
			{
				std::cout << h1.HandCards[1].D[hr][v];
				hr++;
			}
		}
			std::cout << std::endl;
	}
}